package com.typhosoft.photohz2o.app;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.typhosoft.acommon.network.CPhotoConsumer;
import com.typhosoft.acommon.photo.CPathProvider;
import com.typhosoft.acommon.utils.TSFileUtils;
import com.typhosoft.jcommon.bean.CPhoto;
import com.typhosoft.jcommon.bean.CUser;
import com.typhosoft.jcommon.network.CClient;
import com.typhosoft.jcommon.network.CSendActivity;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;


public class MainActivity extends ActionBarActivity {
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private Uri fileUri;
    private CClient client;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText);
        editText.setText("user");
        Button sendButton = (Button) findViewById(R.id.button);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CPhoto photo = new CPhoto();

                photo.setUser(new CUser(editText.getText().toString()));
                try {
                    BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileUri.getPath()));
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    while(in.available()>0)out.write(in.read());
                    photo.setImage(out.toByteArray());
                    new CSendActivity(photo, client.getConnection()).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        Button pictureButton = (Button) findViewById(R.id.pictureButton);

        pictureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                fileUri = TSFileUtils.getOutputMediaFileUri(TSFileUtils.MEDIA_TYPE_IMAGE); // create a file to save the image
                intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
                // start the image capture Intent
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
        });

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        client = new CClient(new CPhotoConsumer(imageView, new CPathProvider()));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                imageView.setVisibility(ImageView.VISIBLE);
                imageView.setImageURI(fileUri);
            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
                imageView.setVisibility(ImageView.INVISIBLE);
            } else {
                // Image capture failed, advise user
                imageView.setVisibility(ImageView.INVISIBLE);
            }
        }
    }
}
